package com.zimi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.model.WfModule;
import com.zimi.service.WfModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: lmc
 * @Date: 2019/3/27 10:34
 * @Description:
 */

@Api("流程模块")
@RestController
@RequestMapping("wf/module")
public class WfModuleApi {

    @Autowired
    private WfModuleService wfModuleService;

    @PostMapping("list")
    @ApiOperation("查询所有")
    public Map getAll(WfModule wfModule) {
        Map returnMap = new HashMap();
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        returnMap.put("rows", wfModuleService.selectAll(wfModule));
        returnMap.put("List", wfModuleService.selectAll(wfModule));
        return returnMap;
    }

    @PostMapping("pageList")
    @ApiOperation("查询所有")
    public Map pageList(WfModule wfModule) {
        PageHelper.startPage(wfModule.getPage(), wfModule.getLimit());
        List<WfModule> list = wfModuleService.selectAll(wfModule);
        PageInfo<WfModule> pageInfo = new PageInfo<>(list);
        Map map = new HashMap();
        map.put("OK", "OK");
        map.put("code", 200);
        map.put("total", pageInfo.getTotal());
        map.put("rows", list);
        return map;
    }

    @PostMapping("findById")
    @ApiOperation("根据Id查询信息")
    public Map findById(int id) {
        Map returnMap = new HashMap();
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        returnMap.put("wfModule", wfModuleService.selectByPrimaryKey(id));
        return returnMap;
    }

    @PostMapping("{id}")
    @ApiOperation("根据Id查询信息")
    public Map selectByPrimaryKey(@PathVariable("id") int id) {
        Map returnMap = new HashMap();
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        returnMap.put("wfModule", wfModuleService.selectByPrimaryKey(id));
        return returnMap;
    }

    @PostMapping("deleteById")
    @ApiOperation("根据Id删除")
    public Object deleteByPrimaryKey(int id) {
        Map returnMap = new HashMap();
        boolean flag = wfModuleService.deleteByPrimaryKey(id) > 0 ? true : false;
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "400");
        }
        return returnMap;
    }

    @PostMapping("add")
    @ApiOperation("添加")
    public Map insert(WfModule wfModule) {
        Map returnMap = new HashMap();
        boolean flag = wfModuleService.insertSelective(wfModule) > 0 ? true : false;
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "400");
        }
        return returnMap;
    }

    @PostMapping("update")
    @ApiOperation("根据条件修改信息")
    public Map updateByPrimaryKey(WfModule wfModule) {
        Map returnMap = new HashMap();
        boolean flag = wfModuleService.updateByPrimaryKeySelective(wfModule) > 0 ? true : false;
        ;
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "400");
        }
        return returnMap;
    }

    @PostMapping("moveUp")
    @ApiOperation("上移")
    public Map moveUp(int id) {
        Map returnMap = new HashMap();
        returnMap.put("msg", wfModuleService.moveUp(id));
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        return returnMap;
    }

    @PostMapping("moveDown")
    @ApiOperation("上移")
    public Map moveDown(int id) {
        Map returnMap = new HashMap();
        returnMap.put("msg", wfModuleService.moveDown(id));
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        return returnMap;
    }

}
